package com.geek.concurrence4work;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.concurrent.*;

/**
 * 1、根据future的方法阻塞拿到
 * <p>
 * Future
 *
 * @author lishuangqiang
 */
public class FutureMethod implements Callable<Long> {

    private static final List NUMS = Lists.newArrayList(0, 1, 2);


    private long sum(int n) {
        int number = 0;
        if (NUMS.contains(n)) {
            return n;
        }
        int numWaysOne = 1;
        int numberWaysTwo = 2;
        for (int i = 2; i < n; i++) {
            number = numWaysOne + numberWaysTwo;
            numWaysOne = numberWaysTwo;
            numberWaysTwo = number;
        }
        return number;
    }


    @Override
    public Long call() throws Exception {
        return sum(36);
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        long start = System.currentTimeMillis();
        // 在这里创建一个线程或线程池，
        // 异步执行 下面方法
        ExecutorService executor = Executors.newFixedThreadPool(1);
        Future<Long> future = executor.submit(new FutureMethod());

        //这是得到的返回值
        long result = future.get();

        // 确保  拿到result 并输出
        System.out.println("异步计算结果为：" + result);

        System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");

        // 然后退出main线程
        executor.shutdown();
    }


}
